<?php
namespace app\index\controller;

use think\Db;
use think\facade\Request;
use think\facade\Config;
use think\facade\Session;
use app\common\traits\Provide;

class Account extends Base
{
    use Provide;
    /**
     * 用户登陆
     */
    public function login(Request $request)
    {
        $room_id = input('room_id');
        if(request()->isAjax())
        {
            $where['name|phone'] = input('user_name');
            $info = Db::name('user')->where($where)->field('password,c_time,u_time', true)->find();
            if($info)
            {
                if(!password_verify(input('password'), $info['password']))
                {
                    return responseJson(400,'账号或者密码错误');
                }
                //清除之前的信息
                $token = Session::pull('user_info');
                $this->redis()->del($token['token']);

                $user_info['user_id']   = $info['id'];
                $user_info['user_name'] = $info['name'];
                $user_info['user_role'] = $info['role'];
                $user_info['speak_state'] = $info['state'];  //发言状态 1正常 0禁言
                $user_info['avatar']    = $info['avatar'];
                $user_info['role_icon'] = $this->getRoleIcon($user_info['user_role']);
                $user_info['room_id']   = $room_id;
                $user_info['token']     = $this->getToken($user_info);
                session('user_info', $user_info);
                return responseJson(200,'登录成功');
            }
        }else{
            $this->assign('room_id', $room_id);
            return $this->fetch('account/login');
        }

    }


    /**
     * 注册 不带手机验证码
     *
     * @param:
     * @return:
     */
    public function register()
    {
        $room_id = input('room_id');
        if(request()->isAjax())
        {
            $data['phone'] = input('phone');
            $info = Db::name('user')->where($data)->value('id');
            if(!$info)
            {
                //清除之前的信息
                $token = Session::pull('user_info');
                $this->redis()->del($token['token']);

                $id = Db::name('user')->order('id desc')->value('id');
                $info['name']   = '用户'.randomString(6);
                $info['phone']  = $data['phone'];
                $info['role']   = 2;
                $info['state']  = 1;
                $info['avatar'] = $this->getAvatar($info['role']);
                $info['password'] = md5(md5(input('password')));
                $info['c_time']  = time();
                Db::name('user')->insert($info);

                $user_info['user_id']   = $id+1;
                $user_info['user_name'] = $info['name'];
                $user_info['user_role'] = $info['role'];;
                $user_info['speak_state'] = $info['state'];  //发言状态 1正常 0禁言
                $user_info['avatar']    = $info['avatar'];
                $user_info['role_icon'] = $this->getRoleIcon($user_info['user_role']);
                $user_info['room_id']   = $room_id;
                $user_info['token']     = $this->getToken($user_info);
                session('user_info', $user_info);
                return responseJson(200,'注册成功');
            }else{
                return responseJson(400,'此手机号已注册');
            }
        }else{
            $this->assign('room_id', $room_id);
            return $this->fetch('account/register');
        }
    }
    /**
     * 退出登录
     *
     * @param:
     * @return:
     */
    public function logout()
    {
        $token = session('user_info',null);
        $this->redis()->del($token['token']);
        $this->redirect('/');
    }
    /**
     * 个人信息
     *
     * @param:
     * @return:
     */
    public function userInfo()
    {
        $user_info = session('user_info');
        if($user_info)
        {
            $this->assign('data', $user_info);
            return $this->fetch('user_info');
        }
    }

    /**
     * 更改用户信息
     *
     * @param:
     * @return:
     */
    public function editUserInfo()
    {
        $user_info = session('user_info');
        if($user_info)
        {
            $edit['name'] = input('name');
            $password = input('password');
            $is_use = Db::name('user')->where('name', $edit['name'])->where('id','neq',$user_info['user_id'])->value('id');
            if($is_use)
            {
                return responseJson(400,'用户名已被使用');
            }
            if($password)
            {
                $edit['password'] = md5(md5($password));
            }
            $edit['u_time'] = time();
            Db::name('user')->where('id', $user_info['user_id'])->update($edit);
            return responseJson(200,'修改成功，请重新登录');
        }else{
            return responseJson(400,'请先登录');
        }
    }
}